<template>
  <div class="xinjian">
    <div class="zhi-top1">
      <van-icon @click="$router.push({name:'Delivery'})" name="arrow-left" />
      <h3>新增收货地址</h3>
    </div>

    <div class="edit-addr-view">
      <van-address-edit
        :area-list="areaList"
        show-set-default
        show-search-result
        :search-result="searchResult"
        @save="onSave"
        @delete="onDelete"
        @change-detail="onChangeDetail"
      />
    </div>
  </div>
</template>
<script>
import Arealist from "../untils/area.js";
import axios from "axios";

export default {
  data() {
    return {
      areaList: Arealist,
      searchResult: []
    };
  },

  methods: {
    onSave() {
      let a = localStorage.getItem("userid");
      let b = localStorage.getItem("token");
      let inputs = document.querySelectorAll(".van-cell");
      console.log(inputs);
      let inp0 = inputs[0].__vue__.value;
      let inp1 = inputs[1].__vue__.value;
      let inp2 = inputs[2].__vue__.value;
      let inp3 = inputs[3].__vue__.value;
      console.log(inp0, inp1, inp2);
      console.log(inp3);
      console.log(inputs);
      console.log(b);
      let moren = document
        .querySelector(".van-switch")
        .getAttribute("aria-checked");
      console.log(moren);

      axios
        .post("http://182.92.222.190:3000/api/v1/address/newAdd", {
          user: a,
          isDefault: moren,
          regions: inp2,
          address: inp3,
          receiver: inp0,
          mobile: inp1,
          headers: {
            yangnb: `ynb ${b}`
          }
        })
        .then(res => {
          console.log(res);
          this.$router.push({ name: "Delivery" });
        });
      this.$toast("保存成功");
    },
    onDelete() {
      this.$toast("删除成功");
    },
    onChangeDetail(val) {
      if (val) {
        this.searchResult = [];
      } else {
        this.searchResult = [];
      }
    }
  }
};
</script>
<style scoped>
@import "../css/xinjian.css";
</style>